package com.example.springcloudwinter.controller;

import com.example.springcloudwinter.dto.ResultEntity;
import com.example.springcloudwinter.service.DeliveryAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RefreshScope
@RestController
@RequestMapping ("/deliveryAddress")
public class DeliveryAddressController {
    
    @Autowired
    private DeliveryAddressService deliveryAddressService;
    
    @PostMapping
    public ResultEntity<?> saveDeliveryAddress (@RequestParam ("userId") String userId,
                                                @RequestParam ("contactName") String name, @RequestParam ("address") String address,
                                                @RequestParam ("contactTel") String phoneNumber, @RequestParam ("contactSex") int sex) {
        return deliveryAddressService.saveDeliveryAddress(userId, name, address, phoneNumber, sex);
    }
    
    @PostMapping ("/{daId}")
    public ResultEntity<?> updateDeliveryAddress (@RequestParam ("userId") String userId, @PathVariable ("daId") int addressId,
                                                  @RequestParam ("contactName") String name, @RequestParam ("address") String address,
                                                  @RequestParam ("contactTel") String phoneNumber, @RequestParam ("contactSex") int sex) {
        return deliveryAddressService.changeDeliveryAddress(name, addressId, userId, address, phoneNumber, sex);
    }
    
    @GetMapping ("/list")
    public ResultEntity<?> getDeliveryAddressByUserId (@RequestParam ("userId") String userId) {
        return deliveryAddressService.getDeliveryAddress(userId);
    }
    
    @GetMapping ("/{daId}")
    public ResultEntity<?> getDeliveryAddressByAddressId (@PathVariable ("daId") int addressId, @RequestParam ("userId") String userId) {
        //此处传入的userId可用于校验该地址是否属于此人
        return deliveryAddressService.getDeliveryAddressByAddressId(addressId, userId);
    }
    
    @PostMapping ("/delete/{daId}")
    public ResultEntity<?> removeDeliveryAddress (@PathVariable ("daId") int addressId, @RequestParam ("userId") String userId) {
        //此处传入的userId可用于校验该地址是否属于此人
        return deliveryAddressService.removeDeliveryAddress(addressId, userId);
    }
}
